package java大项目.a2拼图游戏.util;


import java.util.Random;

public class CodeUtil {
    private CodeUtil() {
    }

    public static String getCode() {
        char arr[] = {'a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'};//因为元素太多了，所以这里只写一部分
        Random r = new Random();
        //添加字母
        char newArr[] = new char[4];
        for (int i = 0; i < newArr.length; i++) {
            int index = r.nextInt(arr.length);
            newArr[i] = arr[index];
        }
        //生成数字
        int numRandom = r.nextInt(10);
        int numindex = r.nextInt(newArr.length);

        //构建新的数组
        StringBuilder sb = new StringBuilder();
        sb.append(newArr).append(numRandom);

        //这里的result是有四个字母和数字的。
        String result = sb.toString();
        char[] charsarr = result.toCharArray();
        //交换
        char temp;
        temp = charsarr[charsarr.length - 1];
        charsarr[charsarr.length - 1] = charsarr[numindex];
        charsarr[numindex] = temp;
        //转换成字符串
        String results = new String(charsarr);
        return result;
    }
}
